package dancrawler.server.gamecmd;

import dancrawler.common.Misc;
import dancrawler.proto.cmd.CmdHandler;
import dancrawler.server.GameHandler;
import dancrawler.server.Player;
import dancrawler.server.netMsg;

/**
 *
 */
public class CmdChatMsg extends CmdHandler {

    protected int[] args = new int[]{-1};
    private GameHandler gh;

    public CmdChatMsg(GameHandler gh) {
        this.gh = gh;
    }

    @Override
    public void handleCmd(netMsg msg) {
        System.out.println("CHATMSG");
        netMsg nmsg;
        nmsg = new netMsg((short) 17, (short) 0, null);
        byte[][] args = new byte[2][];
        args[0] = Misc.shortToBytes((short) gh.getPlayer(msg.getSender().getId()).getId());
        args[1] = new byte[msg.getArgs()[0].length];
        if (args[1].length != 0) {
            System.arraycopy(msg.getArgs()[0], 0, args[1], 0, args[1].length);
        }
        nmsg.setArgs(args);

        for (Player p : gh.getPlayers()) {
            p.getCon().getOutQueue().offer(nmsg);
        }


    }

    @Override
    public int getNumArgs() {
        return args.length;
    }

    @Override
    public int getId() {
        return 16;
    }

    @Override
    public int getArgSize(int n) {
        if (n > args.length - 1 || n < 0) {
            return 0;
        }
        return args[n];
    }

    @Override
    public CmdHandler getHandler() {
        return this;
    }
}
